<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">商品名</text>
			<input class="input" type="text" v-model="productData.name" placeholder="商品名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">商品详情</text>
			<input class="input" type="text" v-model="productData.prodDesc" placeholder="商品详情" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">商品类型</text>
			<input class="input" type="text" @click="showSelect = true" disabled v-model="productData.cateName" placeholder="商品详情" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">商品价格</text>
			<input class="input" type="number" v-model="productData.price" placeholder="商品价格" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">商品库存</text>
			<input class="input" type="number" v-model="productData.stock" placeholder="商品库存" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">商品折扣</text>
			<input class="input" type="number" v-model="productData.discount" placeholder="商品折扣" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">商品排列顺序</text>
			<input class="input" type="number" v-model="productData.sort" placeholder="默认向后排" placeholder-class="placeholder" />
		</view>
		<view class="pic b-b">
			<text class="tit">商店主图</text>
			<u-upload
					:fileList="mainImage"
					compressed
					:maxDuration="60"
					@afterRead="mainSelect"
					@delete="mainDelete"
					name="file"
					accept="video"
					multiple
					:maxCount="1"
				></u-upload>
		</view>
		<view class="pic b-b">
			<view class="tit">商店展示图</view>
			<u-upload
					:fileList="subImages"
					@afterRead="subSelect"
					@delete="subDelete"
					name="file"
					multiple
					:maxCount="4"
					:previewFullImage="true"
					accept="image"
				></u-upload>
		</view>
		<view class="foot">
			<button class="add-btn" @click="submit">提交</button>
		</view>
		<u-picker :show="showSelect" :columns="columns" @confirm="toConfirm" @close="close" keyName="label"></u-picker>
	</view>
</template>

<script>
	import {apiUploadImg} from '@/api/file.js'
	import {apiEditProduct,apiAddProduct,apiGetProductDetail} from '@/api/product.js'
	export default {
		watch:{
			productData:{
				handler(newV){
					if(newV.cate === this.columns[0][0].value){
						this.productData.cateName = '宠物'
					}else if(newV.cate === this.columns[0][1].value){
						this.productData.cateName = '宠物美食'
					}else if(newV.cate === this.columns[0][2].value){
						this.productData.cateName = '宠物服饰'
					}
				},
				immediate:true,
				deep:true
			}
		},
		data() {
			return {
				showSelect:false,
				imageStyles:{
					width:64,
					height:64,
					border:{
						color:"#a5a5a5",
						width:2,
						style:'dashed',
						radius:'2px'
					}
				},
				productData: {
					storeId:'',
					name: '',
					prodDesc: '',
					sort: 0,
					price: 0,
					discount:0,
					cate:0,
					cateName:'宠物',
					mainImage:'',
					subImages:'',
					stock:0,
					productId:'',
					
				},
				columns:[
					[{
						label:'宠物',
						value:0
					},
					{
						label:'宠物美食',
						value:1
					},
					{
						label:'宠物玩具',
						value:2
					}]],
				// 用来保存上面上传图片的信息
				mainImage:[],
				subImages:[],
				headers:{
					Authorization: this.$store.getters.token
				}
			}
		},
		onLoad(option){
			let title = '新增商品';
			this.productData.storeId = option.store_id
			if(option.type==='edit'){
				title = '编辑商品'
				this.productData.productId = option.product_id
				this.getProductData()
			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			getProductData(){
				apiGetProductDetail({'product_id':this.productData.productId}).then(res=>{
					this.productData = res.data
				})
			},
			toConfirm(e){
				this.productData.cate = e.value[0].value
				this.productData.cateName = e.value[0].label
				this.showSelect = false
			},
			close(){
				this.showSelect = false
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					apiUploadImg({name:'file',filePath:url}).then(res=>{
						resolve(res)
					})
				})
			},
			// 三个图片上传的方法
			async mainSelect(event){
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.mainImage.length
				lists.map((item) => {
					this.mainImage.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.mainImage[fileListLen]
					this.mainImage.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url
					}))
					fileListLen++
				}
			},
			mainDelete(event) {
				this.mainImage = []
			},
			async subSelect(event){
				// 可在此处进行图片的压缩
				let lists = [].concat(event.file)
				let fileListLen = this.subImages.length
				lists.map((item) => {
					this.subImages.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.subImages[fileListLen]
					this.subImages.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url
					}))
					fileListLen++
				}
			},
			subDelete(event) {
				this.subImages.splice(event.index, 1)
			},
			submit() {
				this.productData.mainImage = ''
				if(this.mainImage.length > 0){
					this.productData.mainImage = this.mainImage[0].url
				}
				this.productData.subImages = ''
				if(this.subImages.length > 0){
					this.subImages.map(item=>{
						if(!this.productData.subImages){
							this.productData.subImages = item.url
						}else{
							this.productData.subImages = this.productData.subImages + ';' + item.url
						}
					})
				}
				let params = JSON.parse(JSON.stringify(this.productData))
				console.log(params)
				if(this.productData.productId === null || this.productData.productId === ''){
					apiAddProduct(params).then(res=>{
						this.$modal.msg(res.msg)
						// this.$tab.navigateBack()
						
					}).catch(err=>{
						this.$modal.msg('请重新注册')
					})
				}else{
					apiEditProduct(params).then(res=>{
						this.$modal.msg(res.msg)
						// this.$tab.navigateBack()
						
					}).catch(err=>{
						this.$modal.msg('请重新注册')
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: #f8f8f8;
		padding-top: 16upx;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 190upx;
			font-size: 30upx;
			color: #303133;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: #303133;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: #909399;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: 32upx;
		color: #fff;
		background-color: #ecdd00;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 100, 96, 0.4);
	}
	.pic{
		// display: flex;
		// background-color: #fff;
		// align-items: center;
		// justify-content: flex-start;
		padding:0 30upx ;
		background-color: #fff;
		.tit{
			width: 140upx;
		}
	}
</style>

